package com.som.service.excel;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.som.console.CfgEnum;
import com.som.console.ErrorCode;
import com.som.console.ExcelProcException;
import com.som.util.PropertiesUtils;

/**
 * 进行excel的写入操作
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public class FileWriteProcess {
	
	/**
	 * 系统换行符
	 */
	private static final String LINE = PropertiesUtils.getInstance().getValue(CfgEnum.SYSTEM_LINE_FLAG.getCfgMsg());

	/**
	 * 写入数据,从集合中
	 * 
	 * @param path
	 *            定入路径
	 * @param sheetName
	 *            sheet标签页名称
	 * @param cloumnMap
	 *            参数
	 * @param writeList
	 *            写入的集体
	 * @throws ExcelProcException
	 *             异常
	 */
	public void writeByList(String path, List<String> writeList) throws ExcelProcException {
		
		FileWriter fileWrite = null;

		BufferedWriter bufferWrite = null;

		try {
			fileWrite = new FileWriter(path);

			bufferWrite = new BufferedWriter(fileWrite);

			// 进行输出文件内容
			if (null != writeList && !writeList.isEmpty()) {
				for (String line : writeList) {
					bufferWrite.write(line+LINE);
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_NOTFOUNT.getMsg());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ExcelProcException(ErrorCode.FILE_FILE_IS_ERROR.getMsg());
		} finally {
			IOUtils.closeQuietly(bufferWrite);
			IOUtils.closeQuietly(fileWrite);
		}
	}

}
